#include "dbartist.h"
#include "connection.h"
#include <QSqlQuery>
#include <QString>
#include <QSqlError>
#include <QVariant>
#include <QSqlRecord>
#include <QDebug>

DbArtist::DbArtist()
{
}

bool DbArtist::create(QList<DtArtist *> & listArtist)
{
    if(listArtist.isEmpty())
        return true;

    Connection *con = Connection::getInstance();
//    WProgressBar * progress = WProgressBar::getInstance();
//    progress->setMaximumStep(listArtist.size());
//    progress->setText("Inserting artist");
    if(con->isConnected())
    {
        const int MaxBoucle = 999 / 2;
        int i = 0;
        int nbInsert = 0;
        QList<DtArtist *>::ConstIterator itL = listArtist.constBegin();
        QList<DtArtist *>::ConstIterator itG = listArtist.constBegin();
        QSqlQuery query;
        while(itG != listArtist.constEnd())
        {
            QString cmd = "INSERT INTO Artist (name, heart)";
            i = 0;
            for(; itG != listArtist.constEnd() && i < MaxBoucle; itG ++)
            {
//                progress->progress();
                if((*itG)->getId() == -1)
                {
                    if(i == 0)
                        cmd += " SELECT";
                    else
                        cmd += " UNION ALL SELECT";
                    QString n;
                    n.setNum(i);
                    cmd += " :n" + n + ", :h" + n;
                    i ++;
                    nbInsert ++;
                }
            }
            if(i == 0)
                return true;
            if(!query.prepare(cmd))
            {
                qDebug() << query.lastQuery() << query.lastError().text();
                return false;
            }

            i = 0;
            for(; itL != listArtist.constEnd() && i < MaxBoucle; itL ++)
            {
//                progress->progress();
                if((*itL)->getId() == -1)
                {
                    QString n;
                    n.setNum(i);
                    query.bindValue(":n" + n, (*itL)->getName());
                    query.bindValue(":h" + n, (*itL)->getHeart());
                    i ++;
                }
            }
            if(!query.exec())
            {
                qWarning() << query.lastError().text();
            }
            query.finish();

        }
        QSqlQuery q("select max(id) as id from artist");
        if(q.next())
        {
            int lastId = q.value(0).toInt();
            int beforeId = lastId + 1 - nbInsert;
            for(QList<DtArtist *>::iterator itId = listArtist.begin();
                itId != listArtist.end(); itId ++)
            {
                if((*itId)->getId() == -1)
                {
                    (*itId)->setId(beforeId);
                    beforeId++;
                }
            }
        }
//        progress->stop();
        return true;
    }
    return false;
}

bool DbArtist::destroy(const QList<DtArtist *> & listArtist)
{
    if(listArtist.isEmpty())
        return true;

    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "DELETE FROM Artist WHERE id = ?";
        for(int i = 1; i < listArtist.size(); i++)
        {
            requete += " or id = ?";
        }
        QSqlQuery query;
        query.prepare(requete);
        int i = 0;
        for(QList<DtArtist *>::ConstIterator it= listArtist.constBegin();
            it != listArtist.constEnd(); it++)
        {
            query.bindValue(i, (*it)->getId());
            i++;
        }
        if(!query.exec())
        {
            qWarning() << dbArtistStr << dbArtistDestroy
                       << query.lastError().text();
            return false;
        }
        return true;
    }
    return false;
}

bool DbArtist::update(const QList<DtArtist *> &listArtist)
{
    if(listArtist.isEmpty())
        return true;
/*
    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QSqlQuery query;
        query.prepare("UPDATE FROM Artist WHERE id = :id"
                      " set forename = :name, surname = :surname,"
                      " heart = :heart");
        query.bindValue(":id", listArtist.getId());
        query.bindValue(":forename", listArtist.getForename());
        query.bindValue(":surname", listArtist.getSurname());
        query.bindValue(":heart", listArtist.getHeart());
        if(!query.exec())
        {
            Journal::print(Journal::Err, dbArtisteStr, dbArtisteUpdate,
                           query.lastError().text());
            return false;
        }
        return true;
    }
    return false;
*/
    return true;
}

bool DbArtist::getAll(Collection &collection)
{
    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "SELECT id, name, heart FROM artist";
        QSqlQuery query(requete);
        QSqlRecord rec = query.record();

        int idCol = rec.indexOf("id");
        int nameCol = rec.indexOf("name");
        int heartCol = rec.indexOf("heart");
        while(query.next())
        {
            DtArtist *art = new DtArtist(query.value(nameCol).toString(),
                                         query.value(idCol).toInt(),
                                         query.value(heartCol).toBool());
            if(!collection.addArtist(art))
                delete art;
        }
        return true;
    }
    return false;
}

bool DbArtist::setHeart(const DtArtist &artist)
{
    Connection *con = Connection::getInstance();
    if(con->isConnected())
    {
        QString requete = "UPDATE artist SET heart = :heart WHERE id = :id";
        QSqlQuery query;
        query.prepare(requete);
        query.bindValue(":heart", (bool)artist.getHeart() ? 1 : 0);
        query.bindValue(":id", artist.getId());
        if(!query.exec())
        {
            qWarning() << dbArtistStr << query.lastQuery()
                       << query.lastError().text();
            return false;
        }
        return true;
    }
    return false;
}
